# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/8/17 20:45
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : example.py
# @Version: OpenTSN_planner_builder 1.0

import sys

from PySide6.QtCore import QPoint
from PySide6.QtGui import QIcon, QPainter
from PySide6.QtWidgets import QMainWindow, QApplication

from UI.ui_lib.parse_xml import init_topo, get_link_node_port_set
from UI.ui.test_ui import Ui_Form

import qdarkstyle

from UI.ui_lib.ui_structure import TSNButton, DragButton


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.setWindowIcon(QIcon('img/win_logo.png'))
        self.setMouseTracking(True)

        '''下列代码在点击规划按钮的槽函数内执行'''
        topo_xml = '../../tmp/topo_feature.xml'
        # 初始化tsn节点
        node_set = init_topo(topo_xml)
        # 初始化tsn_button
        self.button_set = []
        for node in node_set:
            title = ''
            tsn_button = TSNButton(title,
                                   self,
                                   node['node_id'],
                                   node['node_type'])
            self.button_set.append(tsn_button)

        # 初始化tsn_button的所有子button
        link_node_port_set = get_link_node_port_set(topo_xml)
        for item in link_node_port_set:
            src_node_id = item['src_node_id']
            dst_node_id = item['dst_node_id']
            parent_button = None
            child_button = None
            for button in self.button_set:
                if button.node_id == src_node_id:
                    parent_button = button
                if button.node_id == dst_node_id:
                    child_button = button
            parent_button.appendChildren(child_button)

        # b = TSNButton('', self, '5', 'ES')
        # b = QPushButton('', self)
        b = DragButton('', self, 'test')
        # self.icon = QIcon()
        # self.icon.addFile('../img/sw_mini.png')
        # b.setIcon(self.icon)
        # b.setMaximumSize(40, 40)
        # b.setMinimumSize(40, 40)
        # b.setIconSize(QSize(40, 40))

    # 将节点之间的线添加到图中
    def paintEvent(self, event):
        qp = QPainter(self)
        qp.begin(self)

        for tsn_button in self.button_set:
            parent_tsn_button = tsn_button
            for child_tsn_button in parent_tsn_button.children_button:
                p1_x = parent_tsn_button.point.x() + 20
                p1_y = parent_tsn_button.point.y() + 20
                p2_x = child_tsn_button.point.x() + 20
                p2_y = child_tsn_button.point.y() + 20
                qp.drawLine(QPoint(p1_x, p1_y),
                            QPoint(p2_x, p2_y))
        qp.end()

    # def mouseReleaseEvent(self, event):
    #     if event.buttons() == QtCore.Qt.LeftButton:
    #         self.update()


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()

    # apply_stylesheet(app, theme='dark_teal.xml')
    # https://github.com/UN-GCPDS/qt-material/blob/master/README.md#usage
    app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyside6'))

    window.show()
    sys.exit(app.exec())
